<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.*" %>
<%@ page import="cn.estore.entity.ProductEntity" %>
<%@ page import="cn.estore.dao.ProductDao" %>
<%
    ProductDao productDao = new ProductDao();
    //根据商品名称查询商品记录
    request.setCharacterEncoding("UTF-8");
    String  name = request.getParameter("search").trim();
    List<ProductEntity> list =  productDao.selectProductSearch(name);

    //分页处理：1、计算记录总数
    int pageNumber = list.size();
    //2、计算总页数
    int maxPage = pageNumber;
    if(maxPage % 4 == 0) {  //假设每页显示4条数据
        maxPage = maxPage / 4;
    }else {
        maxPage = maxPage / 4 + 1;
    }
    //3、确定当前显示的是第几页
    String strNumber = request.getParameter("pageNum");
    int number = 0;
    if(strNumber == null) {
        number = 0;
    } else {
        number = Integer.parseInt(strNumber);
    }
    //4、确定获取记录的索引位置
    int start = number * 4;  //开始位置
    int over = (number + 1) * 4;  //结束位置（不包含）
    int count = pageNumber - over; //还剩多少条记录
    if(count <= 0) {
        over = pageNumber;  //避免越界
    }

%>
<html style="width: 1024px; margin: 0 auto">
<head>
    <title>商品展示</title>
</head>
<body>
<table border="0" width="100%">
    <tr>
        <td colspan="2">
            <!--此处插入Banner内容-->
            <jsp:include page="common/head.jsp" flush="true"/>
        </td>
    </tr>
    <tr>
        <td style="width: 30%; vertical-align: top;">
            <!--此处插入菜单选择的内容-->
            <jsp:include page="common/left.jsp" flush="true"/>"
        </td>
        <td width="70%">
            <!--此处插入查询结果内容-->

            <%

                for(int i = start; i < over; i++) {
                    ProductEntity e = (ProductEntity) list.get(i);
            %>
            <table width="95%" height="136" border="1" align="center" cellpadding="1" cellspacing="1" bordercolor="#FFF">
                <tr>
                    <td width="40%" height="80" rowspan="5" bgcolor="#FFF">
                        <div align="center"><img src="<%= "/estore_temp/productImages/" + e.getPicture() %>" width="110" height="100"></div>
                    </td>
                    <td width="59%" bgcolor="#f0f8ff">
                        <div align="left">商品：<%= e.getName()%></div>
                        <div align="left">单价：<%= e.getMarketPrice()%>元</div>
                        <div align="left">简介：<%= e.getDescription()%>
                            登录才能购买
                        </div>
                    </td>
                </tr>

            </table>
            <%
                }
            %>
            <br>
            <table width="95%" height="40" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#FFF">
                <tr align="center">
                    <td width="13%">
                        共<%=maxPage%>页
                    </td>
                    <td width="18%">
                        共有<%=pageNumber%>条记录
                    </td>
                    <td width="26%">
                        当前为第<%=number + 1%>页
                    </td>
                    <td width="15%">
                        <%
                            if((number + 1) == 1) {
                                out.print("上一页");
                            } else {
                                out.print("<a href='"+ request.getContextPath()+"/pages/product/showProductOriginal.jsp?pageNum=" + (number - 1) +"'>上一页</a>");
                            }
                        %>
                    </td>
                    <td width="14%">
                        <%
                            if((number + 1) == maxPage) {
                                out.print("下一页");
                            } else {
                                out.print("<a href='"+ request.getContextPath()+"/pages/product/showProductOriginal.jsp?pageNum=" + (number + 1) +"'>下一页</a>");
                            }
                        %>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
    <tr>
        <td colspan="2">
            <!--此处插入底部内容-->
            <jsp:include page="common/statusBarNavigation.jsp" flush="true"/>
        </td>
    </tr>
</table>
</body>
</html>
